package com.bw.goods.mvvm.viewmodel;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.bw.goods.mvvm.adpter.CategoryLeftAdpter;
import com.bw.goods.mvvm.adpter.CategoryRightAdpter;
import com.bw.goods.mvvm.model.CategoryModel;
import com.bw.goods.mvvm.model.entity.CategoryEntity;
import com.bw.goods.mvvm.model.entity.CategoryValuesEntity;
import com.bw.mvvm_comom.entity.BaseEntity;
import com.bw.mvvm_comom.mvvm.BaseViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class CategoryViewModel extends BaseViewModel<CategoryModel> {

//    List<CategoryEntity> leftlist = new ArrayList<>();
    public CategoryLeftAdpter leadpter =new CategoryLeftAdpter();
    public CategoryRightAdpter rightAdpter = new CategoryRightAdpter();
    private int parentId = 0;
    public int linear;
    public int grid = 1;

    @Override
    public void onCreate() {
        super.onCreate();

        HashMap<String, Object> map = new HashMap<>();
        map.put("parentId", parentId);
        map.put("code",1001);

        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("parentId", 1);
        map2.put("code",1002);

        m.requestCategory(map,map2)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if (baseEntity instanceof CategoryEntity){
            CategoryEntity categoryEntity = (CategoryEntity) baseEntity;
            leadpter.setNewInstance(categoryEntity.data);
            linear = 0;
            leadpter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {

                    HashMap<String, Object> map2 = new HashMap<>();
                    map2.put("parentId", position+1);
                    map2.put("code",1002);


                    m.requestCategory(map2)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(CategoryViewModel.this);
                }
            });
        }
        if (baseEntity instanceof  CategoryValuesEntity){
            CategoryValuesEntity categoryValuesEntity = (CategoryValuesEntity) baseEntity;
            rightAdpter.setNewInstance(categoryValuesEntity.data);
            grid = 1;
        }
    }
}
